package com.ss.android.article.base.utils;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.common.util.json.JsonUtil;
import com.ss.android.image.model.ImageInfo;

import org.json.JSONArray;
import org.json.JSONObject;

public final class ArticleHelper {
    public static boolean a;
    public static int b = -1;
    public static boolean c = false;

    public static String convert2ReadableString(int arg5) {
        int v4 = 10;
        StringBuilder v0 = new StringBuilder();
        int v1 = arg5 / 60;
        int v2 = arg5 % 3600 % 60;
        if (v1 >= v4) {
            v0.append(v1);
        } else if (v1 > 0) {
            v0.append(0);
            v0.append(v1);
        } else {
            v0.append(0);
            v0.append(0);
        }

        v0.append(":");
        if (v2 >= v4) {
            v0.append(v2);
        } else if (v2 > 0) {
            v0.append(0);
            v0.append(v2);
        } else {
            v0.append(0);
            v0.append(0);
        }

        return v0.toString();
    }

    public static void a(Context context, CellRef cellRef, int arg14) {
        long v10 = 0;
        if (context != null && cellRef != null) {
            int v0 = cellRef.ac == null || cellRef.ac.size() <= 0 ? 0 : 1;
            if (v0 == 0) {
                return;
            }

            if (android.support.design.a.isTextEmpty(cellRef.appendExtraData)) {
                return;
            }

            if (arg14 < 0) {
                return;
            }

            if (arg14 < cellRef.ac.size()) {
                DBHelper v7 = DBHelper.getInstance(context);
                if (v7 == null) {
                    return;
                }

                try {
                    Object v0_2 = cellRef.ac.get(arg14);
                    if (((CellRef) v0_2).readTs > v10) {
                        return;
                    }

                    ((CellRef) v0_2).readTs = System.currentTimeMillis();
                    JSONObject v1 = new JSONObject(cellRef.appendExtraData);
                    JSONArray v2 = v1.optJSONArray("data");
                    JSONObject v8 = v2.optJSONObject(arg14);
                    v8.put("read_time_stamp", System.currentTimeMillis());
                    v2.put(arg14, v8);
                    v1.put("data", v2);
                    cellRef.appendExtraData = v1.toString();
                    v7.a(cellRef);
                    if (((CellRef) v0_2).article != null) {
                        ((CellRef) v0_2).article.clickTimestamp = ((CellRef) v0_2).readTs;
                    }

                    long v2_1 = v8.optLong("group_id");
                    long v4 = v8.optLong("item_id");
                    int v6 = v8.optInt("aggr_type");
                    if (v2_1 <= v10) {
                        return;
                    }

                    Article v1_1 = new Article(v2_1, v4, v6);
                    JsonUtil.parseJSONObject(v8, v1_1);
                    v1_1.clickTimestamp = System.currentTimeMillis();
                    v7.syncReadTs(v1_1);
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
    }

    public static int getActualLargeImageHeight(ImageInfo imageInfo, int actualLargeImageWidth, boolean isAd, int maxLargeImageHeight) {
        if (imageInfo == null
                || actualLargeImageWidth <= 0
                || imageInfo.mWidth == 0) {
            return 0;
        } else {
            int actualLargeImageHeight = imageInfo.mHeight * actualLargeImageWidth / imageInfo.mWidth;
            if (!isAd && actualLargeImageHeight > maxLargeImageHeight) {
                actualLargeImageHeight = maxLargeImageHeight;
            }

            if (!isAd) {
                return actualLargeImageHeight;
            }

            if (actualLargeImageHeight <= 3000) {
                return actualLargeImageHeight;
            } else {
                return 3000;
            }
        }
    }

    public static String getSuffixLabel(int arg2, String arg3) {
        String v0 = "";
        if (arg2 == 1) {
            if ("__all__".equals(arg3)) {
                arg3 = "headline";
            } else {
                if (!android.support.design.a.isTextEmpty(arg3)) {
                    return arg3;
                }

                return v0;
            }
        } else if (arg2 == 3) {
            arg3 = "search";
        } else if (arg2 == 4) {
            arg3 = "pgc_list";
        } else if (arg2 == 2) {
            arg3 = "favorite";
        } else if (arg2 == 8) {
            arg3 = "read_history";
        } else if (arg2 == 9) {
            arg3 = "push_history";
        } else if (arg2 == 10) {
            arg3 = "refresh_history";
        } else {
            arg3 = v0;
        }

        return arg3;
    }

    public static void setVisible(ImageView imageView, ImageInfo imageInfo) {
        android.support.design.a.setListImageHolder(imageView);
        if (imageInfo == null || imageInfo.mKey == null) {
            imageView.setTag(null);
            UIUtils.setVisibility(imageView, View.INVISIBLE);
        } else {
            UIUtils.setVisibility(imageView, View.VISIBLE);
            imageView.setEnabled(true);
            imageView.setTag(com.ss.android.article.browser.R.id.image_info, imageInfo);
        }
    }

    public static ImageInfo getImageInfoTag(ImageView imageView) {
        if (imageView == null || imageView.getVisibility() != View.VISIBLE) {
            return null;
        } else {
            Object tag = imageView.getTag(com.ss.android.article.browser.R.id.image_info);
            if ((tag instanceof ImageInfo)) {
                return (ImageInfo) tag;
            } else {
                return null;
            }
        }
    }

    public static void b(ImageView arg1) {
        android.support.design.a.removeImageInfoTag(arg1);
        if (arg1 != null) {
            arg1.setImageDrawable(null);
        }
    }
}

